package glossos.tree;

import com.google.common.base.Predicate;
import glossos.parsing.CharMatcher;

public class MatchPatternPredicate implements Predicate<String> {

	private String[] pattern;

	public MatchPatternPredicate(String pattern) {
		this.pattern = pattern.split("\\s");
	}

	public boolean apply(String str) {
		String[] candidate = str.split("\\s");
		if (candidate.length != pattern.length)
			return false;
		for (int i = 0; i < pattern.length; i++) {
			if (isLiteral(pattern[i])){
				if (!pattern[i].equalsIgnoreCase(candidate[i]))
					return false;
			} 
		}
		return true;
	}

    // TODO The following methods are misplaced. Probably we should create a new abstraction for sentences/patterns.
	public static boolean isValidPattern(String[] pattern){
    	for (String word : pattern) {
			if (!MatchPatternPredicate.isLiteral(word) && !MatchPatternPredicate.isVariable(word))
				return false;
		}
    	return true;
    }    

	public static boolean isLiteral(String string) {
		return CharMatcher.JAVA_LETTER_OR_DIGIT.matchesAllOf(string);
	}

	public static boolean isVariable(String string) {
		return "$".equals(string);
	}

}
